package cn.edu.thu.tsquality.core.algorithm.auto.regression;


import cn.edu.thu.tsquality.adaptor.single.SingleAdaptor;
import cn.edu.thu.tsquality.core.common.algorithm.IAlgorithm;
import cn.edu.thu.tsquality.core.common.util.ArgumentsHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SingleMain
{
    public static void main(String[] args) throws IOException
    {

        System.out.println("AutoRegression");
        System.out.println("args length="+args.length);
        for(int i = 0; i < args.length; i++)
            System.out.println("arg[" + i + "] = " + args[i]);

        String inputPath = args[0];
        String outputPath = args[1];

        Integer regressionSize;

        // ArrayList<Double> coefficients = new ArrayList<>();

        List<String> usedAttrs = null;

        try
        {
            String[] arguments = ArgumentsHelper.getArguments(args[2]);

            if(arguments.length != 1 && arguments.length!=2)
                throw new IllegalArgumentException("argument error, expected 1/2 arguments, got "+arguments.length);

            int argIndex = 0;

            // 如果参数有1个，那么第一个参数是自回归阶数。以整张表作为输入，进行不精确的修复。
            // 如果参数有2个，那么第一个参数是选择列名称，第二个参数是自回归阶数。选择列，进行不完整的修复。
            if(arguments.length == 2)
            {
                String[] attrs = ArgumentsHelper.getValues(arguments[argIndex++]);
                if(attrs.length != 1)
                    throw new IllegalArgumentException("attrs error, expected 1 attr, got "+attrs.length);

                usedAttrs = new ArrayList<>(Arrays.asList(attrs));
            }

            String[] regressionSizeArgs = ArgumentsHelper.getValues(arguments[argIndex++]);
            if(regressionSizeArgs.length != 1)
                throw new IllegalArgumentException("regression size args error, expected 1 arg, got "+regressionSizeArgs.length);

            regressionSize = Integer.parseInt(regressionSizeArgs[0]);

            /*
            String[] coefficientsArgs = ArgumentsHelper.getValues(arguments[argIndex++]);

            if(coefficientsArgs.length == regressionSize + 1)
            {
                for(String coefficient : coefficientsArgs)
                    coefficients.add(Double.parseDouble(coefficient));
            }
            else
            {
                throw new IllegalArgumentException("regressionSize="+regressionSize +", but got "+ coefficientsArgs.length+" coefficients");
            }
            */

            IAlgorithm algorithm = new AutoRegression(regressionSize);
            SingleAdaptor adaptor = new SingleAdaptor();
            adaptor.setAlgorithm(algorithm);

            adaptor.run(inputPath, outputPath, usedAttrs);
        }
        catch (Exception e)
        {
            System.out.println("Catch Exception: "+e.toString());
            throw e;
        }
    }
}

